//
//  LiveListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/9.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "LiveListViewController.h"
#import "LiveClassRoomTableViewCell.h"
#import "LiveDetailViewController.h"

@interface LiveListViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation LiveListViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.title = @"直播列表";
    self.tableView.frame = CGRectMake(0, 0, kScreenW, kScreenH - 64 - 50 - 41);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LiveClassRoomTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"LiveClassRoomCell"];
    [self setupRefresh];//刷新
}
#pragma mark - 获取直播列表
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%ld",(long)self.pageNo] forKey:@"page"];   //页数 默认1
    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:@"1" forKey:@"videoType"];    //课程类型 1直播 2课程
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppCourseGetCourseList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            if (self.pageNo == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray *array = [NSArray arrayWithArray:response];
            NSArray *liveArray = [NSArray arrayWithArray:array[0][@"courseList"][@"list"]];
            for (int i = 0; i < liveArray.count; i++) {
                [self.dataArray addObject:[ClassRoomInfo mj_objectWithKeyValues:liveArray[i]]];
            }
            [self refreshCount:self.dataArray.count noDataText:@"还没有直播课程" totalRow:response[0][@"courseList"][@"totalRow"]];
            
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LiveClassRoomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LiveClassRoomCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.classRoomInfo = self.dataArray[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassRoomInfo *classRoomInfo = self.dataArray[indexPath.section];
    LiveDetailViewController *liveDetailVC = [[LiveDetailViewController alloc]init];
    liveDetailVC.liveDetailId = classRoomInfo.course_id;
    [self.navigationController pushViewController:liveDetailVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 0.01);
//    view.backgroundColor = [UIColor whiteColor];
    
//    UILabel *label = [[UILabel alloc]init];
//    label.text = section ? self.dataArray[section - 1][@"subject"][@"name"] : @"直播课程";
//    label.frame = CGRectMake(20, 12, kScreenW - 40, 20);
//    [view addSubview:label];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = KBACKGROUNDCOLOR;
    view.frame = CGRectMake(0, 0, kScreenW, 0.01);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
